Java Technologies Spring Cloud Gateway এর সাথে Security Integration গাইড ও নোট

308

Spring Cloud Gateway হল একটি API গেটওয়ে যা রিভার্স প্রোক্সি হিসেবে কাজ করে এবং মাইক্রোসার্ভিস আর্কিটেকচারে HTTP রিকোয়েস্ট রাউটিং, লোড ব্যালেন্সিং এবং ফিল্টারিং সমাধান প্রদান করে। এটি Spring Security এর সাথে সহজে ইন্টিগ্রেট করা যায় যাতে আপনি API রিকোয়েস্টগুলোর সুরক্ষা নিশ্চিত করতে পারেন। Spring Cloud Gateway-এর সাথে Security Integration করার মাধ্যমে আপনি Authentication, Authorization, এবং অন্যান্য সিকিউরিটি ফিচার যেমন OAuth2, JWT, Basic Authentication ইত্যাদি প্রয়োগ করতে পারেন।

এখানে আমরা Spring Cloud Gateway এবং Spring Security এর ইন্টিগ্রেশন এবং কিভাবে Authentication এবং Authorization এর জন্য OAuth2, Basic Authentication এবং JWT কনফিগার করা যায় তা নিয়ে বিস্তারিত আলোচনা করব।


1. Spring Cloud Gateway এবং Spring Security Integration

Maven Dependencies (pom.xml):

প্রথমে আপনাকে Spring Cloud Gateway এবং Spring Security এর জন্য প্রয়োজনীয় ডিপেনডেন্সি যুক্ত করতে হবে।

<dependencies>
    <!-- Spring Cloud Gateway -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!-- Spring Security for Authentication & Authorization -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- OAuth2 Login (Optional) -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>

    <!-- JWT Authentication (Optional) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
    </dependency>
</dependencies>

2. Spring Cloud Gateway Configuration for Security

Basic Authentication Example:

Spring Cloud Gateway-এ Basic Authentication কনফিগার করতে হলে Spring Security এর সাহায্যে HTTP Basic Authentication চালু করতে হবে।

application.yml:
spring:
  cloud:
    gateway:
      routes:
        - id: user_route
          uri: http://localhost:8081
          predicates:
            - Path=/users/**
          filters:
            - AddRequestHeader=X-Request-Foo, Bar
  security:
    user:
      name: user
      password: password
  • এখানে Basic Authentication কনফিগার করা হয়েছে, যেখানে user এবং password এর মাধ্যমে Authentication করা হবে।
Security Configuration (SecurityConfig.java):
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/users/**").authenticated() // Protect /users/** endpoints
                .anyRequest().permitAll()
            .and()
            .httpBasic(); // Enable Basic Authentication
    }
}
  • এখানে HTTP Basic Authentication সক্রিয় করা হয়েছে, এবং /users/** এন্ডপয়েন্টে Authentication বাধ্যতামূলক করা হয়েছে।

3. Spring Cloud Gateway and OAuth2 Security Integration

OAuth2 Authentication Example:

Spring Cloud Gateway-এর মাধ্যমে OAuth2 Authorization Code Flow অথবা Client Credentials Flow ব্যবহার করে নিরাপত্তা সুনিশ্চিত করা সম্ভব।

application.yml (OAuth2 Login):
spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: YOUR_CLIENT_ID
            client-secret: YOUR_CLIENT_SECRET
            scope: openid, profile, email
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
            client-name: Google
        provider:
          google:
            authorization-uri: https://accounts.google.com/o/oauth2/auth
            token-uri: https://oauth2.googleapis.com/token
            user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
  • এখানে OAuth2 কনফিগারেশনটি করা হয়েছে, যেখানে Google OAuth2 প্রোভাইডার ব্যবহার করা হচ্ছে। ক্লায়েন্ট-আইডি এবং ক্লায়েন্ট-সিক্রেট পরিবর্তন করতে হবে আপনার OAuth2 প্রোভাইডারের তথ্য অনুসারে।
Security Configuration (SecurityConfig.java):
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableOAuth2Client
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/users/**").authenticated() // Protect /users/** endpoints
                .anyRequest().permitAll()
            .and()
            .oauth2Login(); // Enable OAuth2 Login
        return http.build();
    }
}
  • এখানে OAuth2Login সক্রিয় করা হয়েছে, যা OAuth2 প্রোভাইডার (যেমন, Google) থেকে লগইন করার জন্য ব্যবহৃত হবে।

4. JWT Authentication Example with Spring Cloud Gateway

JWT Authentication Setup:

Spring Cloud Gateway-এ JWT authentication কনফিগার করতে হলে, প্রথমে JWT Token তৈরি এবং এটি যাচাই করতে হবে। এই টোকেনটি সাধারণত Authorization হেডারে পাঠানো হয়।

application.yml (JWT Security):
spring:
  cloud:
    gateway:
      routes:
        - id: user_route
          uri: http://localhost:8081
          predicates:
            - Path=/users/**
          filters:
            - name: JwtAuthenticationFilter

  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:8080/oauth2/jwks  # JWT issuer URI
JWT Authentication Filter:

একটি কাস্টম JWT Authentication Filter তৈরি করতে হবে যা Authorization হেডারে JWT টোকেন যাচাই করবে।

import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;

@Component
public class JwtAuthenticationFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        // Extract JWT from Authorization header and validate
        String token = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
        
        if (token == null || !token.startsWith("Bearer ")) {
            return chain.filter(exchange);
        }

        token = token.substring(7); // Remove "Bearer " prefix

        // JWT Validation logic here...

        return chain.filter(exchange);
    }
}

5. Enabling Global Filters for Security

Spring Cloud Gateway এ global filters ব্যবহার করে আপনি প্রতিটি রিকোয়েস্টে সিকিউরিটি চেক করতে পারেন। এটি Authentication বা Authorization এর জন্য বিশেষভাবে সহায়ক হতে পারে।

Global Filters Example:

import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class SecurityGlobalFilter implements GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, org.springframework.cloud.gateway.filter.GatewayFilterChain chain) {
        String authToken = exchange.getRequest().getHeaders().getFirst("Authorization");
        
        if (authToken == null || !authToken.startsWith("Bearer ")) {
            return Mono.error(new RuntimeException("Unauthorized Access"));
        }
        
        // Token validation logic here

        return chain.filter(exchange);
    }
}
  • এখানে Global Filter ব্যবহার করা হয়েছে যা প্রতিটি রিকোয়েস্টের জন্য Authorization হেডার চেক করে।

Conclusion

Spring Cloud Gateway এর সাথে Spring Security ইন্টিগ্রেট করে আপনি আপনার মাইক্রোসার্ভিস আর্কিটেকচারে শক্তিশালী Authentication এবং Authorization সিস্টেম তৈরি করতে পারেন। এখানে কভার করা হয়েছে Basic Authentication, OAuth2 Login, এবং JWT Authentication। এসব পদ্ধতি প্রয়োগের মাধ্যমে আপনি মাইক্রোসার্ভিসের মধ্যে সিকিউর কনফিগারেশন এবং সিস্টেমের নিরাপত্তা নিশ্চিত করতে পারবেন।

যদি কোন নির্দিষ্ট বিষয় বা কনফিগারেশন সম্পর্কে আরও সাহায্য প্রয়োজন হয়, জানাবেন! 😊

Content added By
Promotion

Are you sure to start over?

Loading...